package com.cz.nongyetongv5.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.bean.ChanPinJianCeRecord;
import com.cz.nongyetongv5.bean.DuChaXunChaRecord;
import com.cz.nongyetongv5.bean.JianChaDanWei;
import com.cz.nongyetongv5.bean.JianChaJieGuo;
import com.cz.nongyetongv5.bean.JianChaRenYuan;
import com.cz.nongyetongv5.bean.JianDuJianChaRecord;
import com.cz.nongyetongv5.bean.Photo;
import com.cz.nongyetongv5.bean.YangPin;
import com.cz.nongyetongv5.custom.wheelview.Utils;
import com.cz.nongyetongv5.utils.CharsetUtil;

public class DaiShangChuanListAdapter extends BaseAdapter {

	// 上下文对象
	private Context context = null;

	/*
	 * 所有的数据
	 */
	private List datas = null;

	/**
	 * 单机事件监听对象
	 */
	private OnClickListener onClickListener = null;

	public DaiShangChuanListAdapter(Context context, List datas,
									OnClickListener onClickListener) {
		this.context = context;
		this.datas = datas;

		this.onClickListener = onClickListener;
	}

	@Override
	public int getCount() {
		return datas == null ? 0 : datas.size();
	}

	@Override
	public Object getItem(int arg0) {
		return datas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	public void setDatas(List<JianDuJianChaRecord> datas) {
		this.datas = datas;
		this.notifyDataSetChanged();
	}
	@Override
	public View getView(int position, View view, ViewGroup parent) {

		ViewGroup daiShangChuanLinearLayout = null;

		if (view == null) {
			daiShangChuanLinearLayout = (ViewGroup) LayoutInflater
					.from(context).inflate(R.layout.daishangchuan_item_layout,
							null);
		} else {
			daiShangChuanLinearLayout = (ViewGroup) view;
		}

		Object data = datas.get(position);

		if (data instanceof JianDuJianChaRecord) {
			// 获得监督检查的记录
			JianDuJianChaRecord jianDuJianChaRecord = (JianDuJianChaRecord) data;

			// 显示照片
			if (jianDuJianChaRecord.getUploadPhoto().size() > 0) {

				Photo firstPhoto = jianDuJianChaRecord.getUploadPhoto().get(0);

				// 如果是有小图的
				if (firstPhoto.getSmallPhotoPath() != null
						&& (firstPhoto.getSmallPhotoPath() instanceof String)) {

					ImageView ivDaiShangChuanImage = (ImageView) daiShangChuanLinearLayout
							.findViewById(R.id.ivDaiShangChuanImage);

					// 进行图片的显示
					BitmapFactory.Options opts = new BitmapFactory.Options();
					opts.inSampleSize = 2;

					Bitmap image = BitmapFactory.decodeFile((String) firstPhoto
							.getSmallPhotoPath());

					ivDaiShangChuanImage.setImageBitmap(image);

					ivDaiShangChuanImage.setScaleType(ScaleType.CENTER_CROP);
				}

			} else {

				// 如果没有则显示签名
				ImageView ivDaiShangChuanImage = (ImageView) daiShangChuanLinearLayout
						.findViewById(R.id.ivDaiShangChuanImage);

				BitmapFactory.Options opts = new BitmapFactory.Options();

				opts.inSampleSize = 2;

				Bitmap image = BitmapFactory.decodeFile(
						jianDuJianChaRecord.getQianMingPath(), opts);

				ivDaiShangChuanImage.setImageBitmap(image);

				ivDaiShangChuanImage.setScaleType(ScaleType.CENTER_CROP);
			}

			try {

				// 时间的设置
				Date time = new SimpleDateFormat("yyyy-MM-dd")
						.parse(jianDuJianChaRecord.getTime());

				TextView tvDaiShangChuanRiQi = (TextView) daiShangChuanLinearLayout
						.findViewById(R.id.tvDaiShangChuanRiQi);

				String timeStr = new SimpleDateFormat("yyyy.MM.dd")
						.format(time);

				tvDaiShangChuanRiQi.setText(timeStr);

			} catch (Exception e) {
			}

			// 设置被检单位
			TextView tvDaiShangChuanDanWei = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.tvDaiShangChuanDanWei);

			tvDaiShangChuanDanWei.setText(jianDuJianChaRecord
					.getBeiJianDanWei() == null ? "" : jianDuJianChaRecord
					.getBeiJianDanWei().getName());

			// 合格数量
			int heGeCount = 0;

			// 不合格数量
			int buHeGeCount = 0;

			// 基本合格数量
			int jiBenHeGeCount = 0;

			// 统计基本合格和不合格的数量
			for (JianChaJieGuo jianChaJieGuo : jianDuJianChaRecord
					.getJianChaJieGuos()) {

				if (jianChaJieGuo != null && jianChaJieGuo.getId() != null) {

					if (buHeGeCount > 0) {

						break;
					}

					if (jianChaJieGuo.getId() == 1) {

						heGeCount++;
					}

					if (jianChaJieGuo.getId() == 2) {

						jiBenHeGeCount++;
					}

					if (jianChaJieGuo.getId() == 3) {

						buHeGeCount++;
					}

				}

			}

			// 继续统计,包括日常监管
			if (jianDuJianChaRecord.getRiChangJianGuan() != null) {

				if (jianDuJianChaRecord.getRiChangJianGuan().getId() == 1) {

					heGeCount++;

				}

				if (jianDuJianChaRecord.getRiChangJianGuan().getId() == 2) {

					jiBenHeGeCount++;
				}

				if (jianDuJianChaRecord.getRiChangJianGuan().getId() == 3) {

					buHeGeCount++;
				}

			}

			String result = "";

			int color;

			if (buHeGeCount > 0) {

				result = "不合格";
				color = Color.parseColor("#ffff0000");
			} else if (jiBenHeGeCount > 0) {
				result = "基本合格";
				color = Color.parseColor("#F4C82D");
			} else {
				result = "合格";
				color = Color.parseColor("#32A35E");
			}

			// 进行检查结果的显示
			TextView tvDaiShangChuanJieGuo = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.tvDaiShangChuanJieGuo);

			tvDaiShangChuanJieGuo.setText(result);

			tvDaiShangChuanJieGuo.setTextColor(color);

			Button btnDaiShangChuanDelete = (Button) daiShangChuanLinearLayout
					.findViewById(R.id.btnDaiShangChuanDelete);
			btnDaiShangChuanDelete.setTag(position);
			btnDaiShangChuanDelete.setOnClickListener(onClickListener);

			Button btnDaiShangChuanEdit = (Button) daiShangChuanLinearLayout
					.findViewById(R.id.btnDaiShangChuanEdit);
			btnDaiShangChuanEdit.setTag(position);
			btnDaiShangChuanEdit.setOnClickListener(onClickListener);

			Button btnDaiShangChuanUpload = (Button) daiShangChuanLinearLayout
					.findViewById(R.id.btnDaiShangChuanUpload);
			btnDaiShangChuanUpload.setTag(position);
			btnDaiShangChuanUpload.setOnClickListener(onClickListener);

			// 遍历添加检查单位
			ViewGroup llJianChaDanWeiLayout = (ViewGroup) daiShangChuanLinearLayout
					.findViewById(R.id.llJianChaDanWeiLayout);

			llJianChaDanWeiLayout.removeAllViews();

			for (int i = 0; i < jianDuJianChaRecord.getJianChaDanWeis().size(); i++) {

				JianChaDanWei jianChaDanWei = jianDuJianChaRecord
						.getJianChaDanWeis().get(i);

				TextView tvJianChaDanWei = new TextView(context);
				tvJianChaDanWei.setLayoutParams(new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				tvJianChaDanWei.setTextColor(Color.parseColor("#36495C"));
				tvJianChaDanWei.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

				int padding = Utils.dip2px(context, 10);

				tvJianChaDanWei.setPadding(padding, padding, padding, padding);

				tvJianChaDanWei.setFocusable(false);
				tvJianChaDanWei.setFocusableInTouchMode(false);
				tvJianChaDanWei.setText(jianChaDanWei.getName());

				llJianChaDanWeiLayout.addView(tvJianChaDanWei);

				// 不是最后一个元素才绘制分割线
				if (i < jianDuJianChaRecord.getJianChaDanWeis().size() - 1) {

					View sperator = new View(context);
					sperator.setLayoutParams(new LayoutParams(
							LayoutParams.MATCH_PARENT, 1));
					sperator.setBackgroundColor(Color.parseColor("#C4C2C0"));
					sperator.setClickable(false);
					llJianChaDanWeiLayout.addView(sperator);

				}

			}

			// 遍历添加检查单位
			TextView tvJianChaRenYuan = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.tvJianChaRenYuan);

			StringBuffer sb = new StringBuffer();

			for (int i = 0; i < jianDuJianChaRecord.getJianChaReYuans().size(); i++) {

				JianChaRenYuan jianChaRenYuan = jianDuJianChaRecord
						.getJianChaReYuans().get(i);

				sb.append(jianChaRenYuan.getName());

				if (i < jianDuJianChaRecord.getJianChaReYuans().size() - 1) {

					sb.append("、\t");
				}
			}

			tvJianChaRenYuan.setText(sb.toString());

		} else if (data instanceof DuChaXunChaRecord) {
			// 获得监督检查的记录
			DuChaXunChaRecord duChaXunChaRecord = (DuChaXunChaRecord) data;

			// 显示照片
			if (duChaXunChaRecord.getUploadPhoto().size() > 0) {

				Photo firstPhoto = duChaXunChaRecord.getUploadPhoto().get(0);

				// 如果是有小图的
				if (firstPhoto.getSmallPhotoPath() != null
						&& (firstPhoto.getSmallPhotoPath() instanceof String)) {

					ImageView ivDaiShangChuanImage = (ImageView) daiShangChuanLinearLayout
							.findViewById(R.id.ivDaiShangChuanImage);

					// 进行图片的显示
					BitmapFactory.Options opts = new BitmapFactory.Options();
					opts.inSampleSize = 2;

					Bitmap image = BitmapFactory.decodeFile((String) firstPhoto
							.getSmallPhotoPath());

					ivDaiShangChuanImage.setImageBitmap(image);

					ivDaiShangChuanImage.setScaleType(ScaleType.CENTER_CROP);
				}

			} else {

				// 如果没有则显示签名
				ImageView ivDaiShangChuanImage = (ImageView) daiShangChuanLinearLayout
						.findViewById(R.id.ivDaiShangChuanImage);

				BitmapFactory.Options opts = new BitmapFactory.Options();

				opts.inSampleSize = 2;

				Bitmap image = BitmapFactory.decodeFile(
						duChaXunChaRecord.getQianMingPath(), opts);

				ivDaiShangChuanImage.setImageBitmap(image);

				ivDaiShangChuanImage.setScaleType(ScaleType.CENTER_CROP);
			}

			try {

				// 时间的设置
				Date time = new SimpleDateFormat("yyyy-MM-dd")
						.parse(duChaXunChaRecord.getTime());

				TextView tvDaiShangChuanRiQi = (TextView) daiShangChuanLinearLayout
						.findViewById(R.id.tvDaiShangChuanRiQi);

				String timeStr = new SimpleDateFormat("yyyy.MM.dd")
						.format(time);

				tvDaiShangChuanRiQi.setText(timeStr);

			} catch (Exception e) {
			}

			// 设置被检单位
			TextView tvDaiShangChuanDanWei = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.tvDaiShangChuanDanWei);

			tvDaiShangChuanDanWei.setText(duChaXunChaRecord
					.getBeiJianDanWei() == null ? "" : duChaXunChaRecord
					.getBeiJianDanWei().getName());

			// 合格数量
			int heGeCount = 0;

			// 不合格数量
			int buHeGeCount = 0;

			// 基本合格数量
			int jiBenHeGeCount = 0;

			// 统计基本合格和不合格的数量
			for (JianChaJieGuo jianChaJieGuo : duChaXunChaRecord
					.getJianChaJieGuos()) {

				if (jianChaJieGuo != null && jianChaJieGuo.getId() != null) {

					if (buHeGeCount > 0) {

						break;
					}

					if (jianChaJieGuo.getId() == 1) {

						heGeCount++;
					}

					if (jianChaJieGuo.getId() == 2) {

						jiBenHeGeCount++;
					}

					if (jianChaJieGuo.getId() == 3) {

						buHeGeCount++;
					}

				}

			}


			String result = "";

			int color;

			if (buHeGeCount > 0) {

				result = "不合格";
				color = Color.parseColor("#ffff0000");
			} else if (jiBenHeGeCount > 0) {
				result = "基本合格";
				color = Color.parseColor("#F4C82D");
			} else {
				result = "合格";
				color = Color.parseColor("#32A35E");
			}

			// 进行检查结果的显示
			TextView tvDaiShangChuanJieGuo = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.tvDaiShangChuanJieGuo);

			tvDaiShangChuanJieGuo.setText(result);

			tvDaiShangChuanJieGuo.setTextColor(color);

			Button btnDaiShangChuanDelete = (Button) daiShangChuanLinearLayout
					.findViewById(R.id.btnDaiShangChuanDelete);
			btnDaiShangChuanDelete.setTag(position);
			btnDaiShangChuanDelete.setOnClickListener(onClickListener);

			Button btnDaiShangChuanEdit = (Button) daiShangChuanLinearLayout
					.findViewById(R.id.btnDaiShangChuanEdit);
			btnDaiShangChuanEdit.setTag(position);
			btnDaiShangChuanEdit.setOnClickListener(onClickListener);

			Button btnDaiShangChuanUpload = (Button) daiShangChuanLinearLayout
					.findViewById(R.id.btnDaiShangChuanUpload);
			btnDaiShangChuanUpload.setTag(position);
			btnDaiShangChuanUpload.setOnClickListener(onClickListener);

			// 遍历添加检查单位
			ViewGroup llJianChaDanWeiLayout = (ViewGroup) daiShangChuanLinearLayout
					.findViewById(R.id.llJianChaDanWeiLayout);

			llJianChaDanWeiLayout.removeAllViews();

			for (int i = 0; i < duChaXunChaRecord.getJianChaDanWeis().size(); i++) {

				JianChaDanWei jianChaDanWei = duChaXunChaRecord
						.getJianChaDanWeis().get(i);

				TextView tvJianChaDanWei = new TextView(context);
				tvJianChaDanWei.setLayoutParams(new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				tvJianChaDanWei.setTextColor(Color.parseColor("#36495C"));
				tvJianChaDanWei.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

				int padding = Utils.dip2px(context, 10);

				tvJianChaDanWei.setPadding(padding, padding, padding, padding);

				tvJianChaDanWei.setFocusable(false);
				tvJianChaDanWei.setFocusableInTouchMode(false);
				tvJianChaDanWei.setText(jianChaDanWei.getName());

				llJianChaDanWeiLayout.addView(tvJianChaDanWei);

				// 不是最后一个元素才绘制分割线
				if (i < duChaXunChaRecord.getJianChaDanWeis().size() - 1) {

					View sperator = new View(context);
					sperator.setLayoutParams(new LayoutParams(
							LayoutParams.MATCH_PARENT, 1));
					sperator.setBackgroundColor(Color.parseColor("#C4C2C0"));
					sperator.setClickable(false);
					llJianChaDanWeiLayout.addView(sperator);

				}

			}

			// 遍历添加检查单位
			TextView tvJianChaRenYuan = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.tvJianChaRenYuan);

			StringBuffer sb = new StringBuffer();

			for (int i = 0; i < duChaXunChaRecord.getJianChaReYuans().size(); i++) {

				JianChaRenYuan jianChaRenYuan = duChaXunChaRecord
						.getJianChaReYuans().get(i);

				sb.append(jianChaRenYuan.getName());

				if (i < duChaXunChaRecord.getJianChaReYuans().size() - 1) {

					sb.append("、\t");
				}
			}

			tvJianChaRenYuan.setText(sb.toString());

		}else if (data instanceof ChanPinJianCeRecord) {

			ChanPinJianCeRecord chanPinJianCeRecord = (ChanPinJianCeRecord) data;

			// 显示照片
			if (chanPinJianCeRecord.getUploadPhoto().size() > 0) {

				Photo firstPhoto = chanPinJianCeRecord.getUploadPhoto().get(0);

				// 如果是有小图的
				if (firstPhoto.getSmallPhotoPath() != null
						&& (firstPhoto.getSmallPhotoPath() instanceof String)) {

					ImageView ivDaiShangChuanImage = (ImageView) daiShangChuanLinearLayout
							.findViewById(R.id.ivDaiShangChuanImage);

					// 进行图片的显示
					BitmapFactory.Options opts = new BitmapFactory.Options();
					opts.inSampleSize = 2;

					Bitmap image = BitmapFactory.decodeFile((String) firstPhoto
							.getSmallPhotoPath());

					ivDaiShangChuanImage.setImageBitmap(image);

					ivDaiShangChuanImage.setScaleType(ScaleType.CENTER_CROP);
				}

			} else {

				// 如果没有则显示签名
				ImageView ivDaiShangChuanImage = (ImageView) daiShangChuanLinearLayout
						.findViewById(R.id.ivDaiShangChuanImage);

				BitmapFactory.Options opts = new BitmapFactory.Options();

				opts.inSampleSize = 2;

				Bitmap image = BitmapFactory.decodeFile(
						chanPinJianCeRecord.getQianMingPath(), opts);

				ivDaiShangChuanImage.setImageBitmap(image);

				ivDaiShangChuanImage.setScaleType(ScaleType.CENTER_CROP);
			}

			// 时间的设置
			TextView tvDaiShangChuanRiQi = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.tvDaiShangChuanRiQi);

			tvDaiShangChuanRiQi.setText(chanPinJianCeRecord.getTime());

			// 设置被检单位
			TextView tvDaiShangChuanDanWei = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.tvDaiShangChuanDanWei);
			if(chanPinJianCeRecord
					.getShouJianDanWei()==null){
				tvDaiShangChuanDanWei.setText("未知单位");
			}else
				tvDaiShangChuanDanWei.setText(chanPinJianCeRecord
						.getShouJianDanWei().getName());

			// 设置样品信息
			TextView tvDaiShangChuanJieGuo = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.tvDaiShangChuanJieGuo);

			String yangPinXinXi = "";

			for (YangPin yp : chanPinJianCeRecord.getYangPins()) {

				yangPinXinXi += yp.getName() + "，";
			}

			if (CharsetUtil.isNotNull(yangPinXinXi)) {

				yangPinXinXi = yangPinXinXi.substring(0,
						yangPinXinXi.length() - 1);

			}

			tvDaiShangChuanJieGuo.setText(yangPinXinXi);

			TextView tvTitle1 = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.title1);

			tvTitle1.setText("抽验机构");

			TextView tvTitle2 = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.title2);

			tvTitle2.setText("抽验人员");

			Button btnDaiShangChuanDelete = (Button) daiShangChuanLinearLayout
					.findViewById(R.id.btnDaiShangChuanDelete);
			btnDaiShangChuanDelete.setTag(position);
			btnDaiShangChuanDelete.setOnClickListener(onClickListener);

			Button btnDaiShangChuanEdit = (Button) daiShangChuanLinearLayout
					.findViewById(R.id.btnDaiShangChuanEdit);
			btnDaiShangChuanEdit.setTag(position);
			btnDaiShangChuanEdit.setOnClickListener(onClickListener);

			Button btnDaiShangChuanUpload = (Button) daiShangChuanLinearLayout
					.findViewById(R.id.btnDaiShangChuanUpload);
			btnDaiShangChuanUpload.setTag(position);
			btnDaiShangChuanUpload.setOnClickListener(onClickListener);

			// 遍历添加抽验机构
			ViewGroup llJianChaDanWeiLayout = (ViewGroup) daiShangChuanLinearLayout
					.findViewById(R.id.llJianChaDanWeiLayout);

			llJianChaDanWeiLayout.removeAllViews();

			// 画出机构的名字
			TextView tvJianChaDanWei = new TextView(context);

			tvJianChaDanWei.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			tvJianChaDanWei.setTextColor(Color.parseColor("#36495C"));
			tvJianChaDanWei.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);

			int padding = Utils.dip2px(context, 10);

			tvJianChaDanWei.setPadding(padding, padding, padding, padding);

			tvJianChaDanWei.setFocusable(false);
			tvJianChaDanWei.setFocusableInTouchMode(false);
			tvJianChaDanWei.setText(chanPinJianCeRecord.getChouYanJiGou()
					.getName());

			llJianChaDanWeiLayout.addView(tvJianChaDanWei);

			// 遍历添加检查单位
			TextView tvJianChaRenYuan = (TextView) daiShangChuanLinearLayout
					.findViewById(R.id.tvJianChaRenYuan);

			StringBuffer sb = new StringBuffer();

			for (int i = 0; i < chanPinJianCeRecord.getChouYangRenYuans()
					.size(); i++) {

				JianChaRenYuan jianChaRenYuan = chanPinJianCeRecord
						.getChouYangRenYuans().get(i);

				sb.append(jianChaRenYuan.getName());

				if (i < chanPinJianCeRecord.getChouYangRenYuans().size() - 1) {

					sb.append("、\t");
				}
			}

			tvJianChaRenYuan.setText(sb.toString());

		}

		return daiShangChuanLinearLayout;
	}

	/**
	 * 删除
	 *
	 * @param position
	 */
	public void remove(int position) {
		this.datas.remove(position);
		this.notifyDataSetChanged();
	}

	public void remove(Object record) {
		remove(datas.indexOf(record));
	}

}
